/*
 * @Author: wangtao
 * @Date: 2020-08-20 14:07:42
 * @LastEditors: kanglang
 * @LastEditTime: 2021-08-03 19:02:15
 * @Description:
 */

import React from 'react';
import {
  View,
  StyleSheet,
  Text,
  TouchableOpacity,
} from 'react-native';
import { msg, Check } from '@/common';
import baseConfig from '@/config/baseConfig';
import { px2dp, fontColorCoffee } from '@/styles';
import { getAgreementByArray } from '@/common/agreement-version';

const { terminalCode } = baseConfig;
export default class ChooseAgreement extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      agreementList: []
    };
  }

  componentDidMount() {
    // 货物运输协议
    getAgreementByArray(
      [{ appCode: terminalCode, code: 'logiticsAgreement', }],
      (data) => {
        this.setState({ agreementList: data });
      },
    );
  }

  render() {
    const { isAgree, onChange } = this.props;
    const { agreementList } = this.state;
    return (
      <View
        style={{
          width: '100%',
          flexDirection: 'row',
          marginTop: px2dp(24),
          marginLeft: px2dp(32),
          alignItems: 'center',
        }}
      >
        <Check
          checked={isAgree}
          onCheck={() => {
            onChange('isAgree', !isAgree);
          }}
        />
        <Text style={{ fontSize: px2dp(24), marginLeft: 4 }}>我已阅读并同意</Text>
        {agreementList.map((item) => {
          if (!item || JSON.stringify(item) === '{}') return;
          return <TouchableOpacity
            activeOpacity={0.5}
            onPress={() => this.jumpAgreement(item)}
          >
            <Text style={{ color: fontColorCoffee, fontSize: px2dp(24) }}>
              {`《${item.title}》`}
            </Text>
          </TouchableOpacity>;
        })}

      </View>
    );
  }

  // 货物运输协议
  jumpAgreement = (item) => {
    msg.emit('router: goToNext', {
      routeName: 'CustAgreement',
      title: item.title,
      content: item.content
    });
  };
}

const styles = StyleSheet.create({});
